package org.hitchhike.web.front.controller;

import javax.validation.Valid;

import org.hitchhike.common.domain.BaseEntity;
import org.hitchhike.common.web.controller.BaseController;
import org.hitchhike.core.domain.Order;
import org.hitchhike.core.domain.User;
import org.hitchhike.core.exception.BaseException;
import org.hitchhike.core.form.OrderForm;
import org.hitchhike.core.service.CitiesService;
import org.hitchhike.core.service.OrderService;
import org.hitchhike.core.service.ProvincesService;
import org.hitchhike.core.service.UserService;
import org.hitchhike.core.utils.PropertiesUtils;
import org.hitchhike.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/front")
public class FrontController extends BaseController<BaseEntity, Long> {

  @Autowired
  private UserService userService;
  @Autowired
  private ProvincesService provincesService;
  @Autowired
  private CitiesService citiesService;
  @Autowired
  private OrderService orderService;
  @Autowired
  private PropertiesUtils propertiesUtils;

  @RequestMapping(method = RequestMethod.GET)
  public String main(Model model) {
    model.addAttribute("orderList", orderService.findToFrontMain());
    return viewName("main");
  }

  @RequestMapping(value = "/fabupinche", method = RequestMethod.GET)
  public String fabupinche(Model model) {
    model.addAttribute("provincesList", provincesService.findAll());
    model.addAttribute("hhs", DateUtil.getHH());
    model.addAttribute("mms", DateUtil.getMM());
    model.addAttribute("orderPrice", propertiesUtils.getOrderPrice());
    return viewName("fabupinche");
  }

  @RequestMapping(value = {"/chepiao", "/qiangdan"}, method = RequestMethod.GET)
  public String chepiao(Model model, OrderForm orderForm, Integer page) {
    model.addAttribute("page", orderService.findPageTochepiao(page, orderForm));
    model.addAttribute("provincesList", provincesService.findAll());
    return viewName("chepiao");
  }

  @RequestMapping(value = "/save", method = RequestMethod.POST)
  public String save(@Valid Order order) {
    User user = userService
        .findByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
    order.setUser(user);
    order.setStartProvinces(
        provincesService.findByProvinceid(order.getStartProvinces().getProvinceid()));
    order.setStopProvinces(
        provincesService.findByProvinceid(order.getStopProvinces().getProvinceid()));
    order.setStartCities(citiesService.findByCityid(order.getStartCities().getCityid()));
    order.setStopCities(citiesService.findByCityid(order.getStopCities().getCityid()));
    order.setGoTime(order.getGoHH() + ":" + order.getGoMM());
    try {
      orderService.save(order);
    } catch (Exception e) {
      throw new BaseException(e.getMessage());
    }
    return redirectToUrl(viewName("/order/12"));
  }

}
